package com.alipay.service.impl;

import com.alipay.config.AlipayConfig;
import com.alipay.domain.Alipay;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.alipay.service.AlipayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlipayServiceImpl implements AlipayService {

    @Autowired
    private AlipayConfig alipayConfig;

    /**
     * PC网页支付
     */
    @Override
    public String aliPayPc(Alipay alipay) {
        log.info("支付宝PC支付下单");
        try {
            AlipayTradePagePayResponse response = Factory.Payment.Page()
                    .asyncNotify(alipayConfig.getNotifyUrl())
                    .pay(alipay.getSubject(), alipay.getTraceNo(), alipay.getTotalAmount() ,alipayConfig.getReturnUrl());
            if (!ResponseChecker.success(response)) {
                return "fail";
            } else {
                return response.getBody();
            }
        } catch (Exception e) {
            log.error("支付宝下单失败，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
